﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using yncTech.BusinessObjects;
using yncTech.Common;
using EntitySpaces.Interfaces;

public partial class Admin_SAPFin_SAPFin002e : AdminBasePage
{
    decimal[] ids;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["ids"] != null)
        {
            ids = Request.QueryString["ids"].TrimEnd(',').Split(',').Select(r => decimal.Parse(r)).ToArray();
        }
        if (!IsPostBack)
        {
            if (ids.Length > 0)
            {
                if (ids.Length == 1)
                {
                    FinReceipt finReceipt = new FinReceipt();
                    if (finReceipt.LoadByPrimaryKey(ids[0]))
                    {
                        drpEStorageStatus.SelectedValue = finReceipt.EStorageStatus;
                        txtEStorageLocation.Text = finReceipt.EStorageLocation;
                        txtComments.Text = finReceipt.Comments;

                    }
                }
                else
                {
                    btnSave.Text = "批量修改";
                }
            }
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            lbMessage.Text = "";
            FinReceiptCollection receipts = new FinReceiptCollection();
            receipts.Query.Where(receipts.Query.ReceiptID.In(ids));
            receipts.Query.Load();
            using (esTransactionScope tr = new esTransactionScope())
            {
                foreach (var receipt in receipts)
                {
                    string content = string.Empty;
                    
                    if (receipt.EStorageStatus != drpEStorageStatus.SelectedValue)
                    {
                        if (string.IsNullOrEmpty(receipt.EStorageStatus))
                        {
                            content += "电子存储状态从未填写改成为" + Business.getCodeTableValue(drpEStorageStatus.SelectedValue) + "\r\n";
                        }
                        else
                        {
                            content += "电子存储状态从" + Business.getCodeTableValue(receipt.EStorageStatus) + "改成为" + Business.getCodeTableValue(drpEStorageStatus.SelectedValue) + "\r\n";
                        }
                        receipt.EStorageStatus = drpEStorageStatus.SelectedValue;
                    }

                    if (receipt.EStorageLocation != txtEStorageLocation.Text.Trim())
                    {
                        if (string.IsNullOrEmpty(receipt.EStorageLocation))
                        {
                            content += "电子存储状态从未填写改成为" + txtEStorageLocation.Text.Trim() + "\r\n";
                        }
                        else
                        {
                            content += "电子存储地点从" + receipt.EStorageLocation + "改成为" + txtEStorageLocation.Text.Trim() + "\r\n";
                        }

                        receipt.EStorageLocation = string.IsNullOrEmpty(txtEStorageLocation.Text.Trim()) ? "N/A" : txtEStorageLocation.Text.Trim();
                    }

                    if (receipt.Comments != txtComments.Text.Trim())
                    {
                        if (string.IsNullOrEmpty(receipt.EStorageLocation))
                        {
                            content += "备注从未填写改成为" + txtComments.Text.Trim() + "\r\n";
                        }
                        else
                        {
                            content += "备注从" + receipt.Comments + "改成为" + txtComments.Text.Trim() + "\r\n";
                        }


                        receipt.Comments = string.IsNullOrEmpty(txtComments.Text.Trim()) ? "N/A" : txtComments.Text.Trim();
                    }


                    

                    FinReceiptHis finReceiptHis = new FinReceiptHis();
                    finReceiptHis.Content = content;
                    finReceiptHis.ReceiptId = receipt.ReceiptID;
                    finReceiptHis.CreateUser = LoginUserInfo.CurrentUser.UserName;
                    finReceiptHis.CreateDate = DateTime.Now;
                    finReceiptHis.Save();

                    receipt.UpdatedBy = LoginUserInfo.CurrentUser.UserName;
                    receipt.UpdatedDttm = DateTime.Now;
                    receipt.TransactionID = Guid.NewGuid().ToString();
                    receipt.VersionNo = receipt.VersionNo + 1;
                }

                receipts.Save();
                tr.Complete();
            }

            WebHelper.CloseDialog(true);
        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }

    }
}